import { createRouter, createWebHistory } from "vue-router";
import Home from "../components/home/Home.vue";
import Admin from "../components/admin/Admin.vue";


const routes = [
  {
	  path: "/",
	  // redirect:{
	  //  name: "Home"
	  // }
	  redirect:'/home'
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
	children:[
		{
		  path: "",
		  name: "Main",
		  component: () =>
		    import(/* webpackChunkName: "main" */ "../components/home/pages/Main/Main.vue"),
		},
		{
		  path: "detail",
		  name: "Detail",
		  component: () =>
		    import(/* webpackChunkName: "detail" */ "../components/home/pages/Detail/Detail.vue"),
		},
		{
		  path: "cart",
		  name: "Cart",
		  component: () =>
		    import(/* webpackChunkName: "cart" */ "../components/home/pages/Cart/Cart.vue"),
			meta:{
				role:'P'
			}
		},
		{
		  path: "orders",
		  name: "Orders",
		  component: () =>
		    import(/* webpackChunkName: "orders" */ "../components/home/pages/Orders/Orders.vue"),
			meta:{
				role:'P'
			}
		},
		{
		  path: "make",
		  name: "Make",
		  component: () =>
		    import(/* webpackChunkName: "make" */ "../components/home/pages/Orders/Make.vue"),
			meta:{
				role:'P'
			}
		},
		{
		  path: "pay",
		  name: "Pay",
		  component: () =>
		    import(/* webpackChunkName: "pay" */ "../components/home/pages/Orders/Pay.vue"),
			meta:{
				role:'P'
			}
		},
		{
		  path: "information",
		  name: "Information",
		  component: () =>
		    import(/* webpackChunkName: "information" */ "../components/home/pages/Information/Information.vue"),
			meta:{
				role:'P'
			}
		},
	]
  },
  {
    path: "/admin",
    name: "Admin",
    component:Admin,
	children:[
		{
		  path: "type",
		  name: "Type",
		  component: () =>
		    import(/* webpackChunkName: "type" */ "../components/admin/pages/Type.vue"),
		},
		{
		  path: "books",
		  name: "Books",
		  component: () =>
		    import(/* webpackChunkName: "books" */ "../components/admin/pages/Books.vue"),
		},
		{
		  path: "banner",
		  name: "Banner",
		  component: () =>
		    import(/* webpackChunkName: "banner" */ "../components/admin/pages/Banner.vue"),
		},
		{
		  path: "bookedit",
		  name: "Bookedit",
		  component: () =>
		    import(/* webpackChunkName: "bookedit" */ "../components/admin/pages/Bookedit.vue"),
		},
		{
		  path: "hotedit",
		  name: "Hotedit",
		  component: () =>
		    import(/* webpackChunkName: "hotedit" */ "../components/admin/pages/Hotedit.vue"),
		},
		{
		  path: "order",
		  name: "Order",
		  component: () =>
		    import(/* webpackChunkName: "order" */ "../components/admin/pages/Order.vue"),
			
		},
		{
		  path: "list",
		  name: "List",
		  component: () =>
		    import(/* webpackChunkName: "list" */ "../components/admin/pages/List.vue"),
		},
		{
		  path: "add",
		  name: "Add",
		  component: () =>
		    import(/* webpackChunkName: "add" */ "../components/admin/pages/Add.vue"),
		},
	]
	},
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach((to,from,next)=>{
	let role=localStorage.getItem('role')
	
	if (to.meta.role==undefined||to.meta.role=='') {
		next()
		return
	} 
	
	//判断即将进入的路由规则是否与当前登录者的角色匹配
	if (to.meta.role==role) {
		next()
		return
	}
	router.replace('/home').catch((error)=>{
		console.log('地址不存在')
	})
})

export default router;
